//
//  WXPBarCodeView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/19.
//

#import "WXPBarCodeView.h"

@implementation WXPBarCodeView

#pragma mark - lazy
- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = COLOR_BLACK;
        _titleLabel.fontNum = (16);
    }
    return _titleLabel;
}
- (UILabel *)timeLabel{
    if (_timeLabel == nil) {
        _timeLabel = [UILabel new];
        _timeLabel.textColor = COLOR_PRIMARY;
        _timeLabel.fontNum = (16);
    }
    return _timeLabel;
}
- (UIImageView *)barCodeImageView{
    if (_barCodeImageView == nil) {
        _barCodeImageView = [UIImageView new];
        _barCodeImageView.widthHeight = XY((200), (66));
    }
    return _barCodeImageView;
}
- (WKWebView *)webView
{
    if (_webView == nil) {
        _webView = [WKWebView  new];
        _webView.widthHeight = XY((200), (66));
        _webView.backgroundColor = COLOR_PRIMARY;
        _webView.scrollView.scrollEnabled = false;
    }
    return  _webView;
}
- (UILabel *)codeLabel{
    if (_codeLabel == nil) {
        _codeLabel = [UILabel new];
        _codeLabel.textColor = COLOR_CONTENT;
        _codeLabel.fontNum = (12);
    }
    return _codeLabel;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];//背景色
        self.width = SCREEN_WIDTH;//默认宽度
        [self addSubView];//添加子视图
    }
    return self;
}

//添加subview
- (void)addSubView{
//    [self addSubview:self.titleLabel];
//    [self addSubview:self.timeLabel];
//    [self addSubview:self.barCodeImageView];
    [self addSubview:self.webView];
    [self addSubview:self.codeLabel];
    //初始化页面
    [self resetViewWithModel:nil];
}

#pragma mark - 刷新view
- (void)resetViewWithModel:(ModelRepaymentCodeList *)model{
    // 刷新view
    // 要去文描和下面的时间隐藏
//    [self.titleLabel fitTitle:@"Fecha límite de pago" variable:0];
//    self.titleLabel.centerXTop = XY(SCREEN_WIDTH/2, 0);
//
//    [self.timeLabel fitTitle:[self changeTimestamp:model.expireTime] variable:0];
//    self.timeLabel.centerXTop = XY(SCREEN_WIDTH/2, self.titleLabel.bottom+(8));
    
//    self.webView.centerXTop = XY(SCREEN_WIDTH/2, self.timeLabel.bottom+(16));
    self.webView.centerXTop = XY(SCREEN_WIDTH/2, 0);
//    self.barCodeImageView.centerXTop = XY(SCREEN_WIDTH/2, self.timeLabel.bottom+(16));
    [self loadingBarCodeWithUrl:model.barcodeUrlAlfa];
    
    [self.codeLabel fitTitle:readStr(model.paymentCode) variable:0];
    self.codeLabel.centerXTop = XY(SCREEN_WIDTH/2, self.webView.bottom+(8));

    self.height = self.codeLabel.bottom;
}

- (void)loadingBarCodeWithUrl:(NSString *)urlStr{
    if(!isStr(urlStr)){
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:readStr(urlStr)]]];
    });
}

- (NSString *)changeTimestamp:(double)times{
    // 定义时间戳
    NSTimeInterval timestamp = times/1000;

    // 创建NSDate对象
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];

    // 创建NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    // 设置日期和时间格式
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"es_ES"]]; // 设置为西班牙语环境
    [dateFormatter setDateFormat:@"d 'de' MMMM yyyy, 'a las' h:mm a"];
    
    // 转化为字符串
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    NSLog(@"%@", formattedDate); // 输出：30 de March 2023, a las 2:30 AM
    return formattedDate;
}


@end
